﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Navigation;

namespace FasterWPF
{
    public static class TabItemExt
    {
        public static ControlTemplate ApplyTabItemStrategy(this TabItem tbc, string tabBorderBrush, string tabBackgroundColor, string tabMouseOverBorderBrush, string tabMouseOverColor, string tabItemSelectedBackground, string tabItemDisabledBackground, string tabItemDisabledBorderBrush)
        {
            string xaml = "<ControlTemplate TargetType=\"{x:Type TabItem}\"><Grid SnapsToDevicePixels=\"true\"><Border Name=\"Bd\" Padding=\"{TemplateBinding Padding}\" BorderBrush=\"" + tabBorderBrush + "\" Background=\"" + tabBackgroundColor + "\" BorderThickness=\"1,1,1,0\"><ContentPresenter Name=\"Content\" ContentSource=\"Header\" SnapsToDevicePixels=\"{TemplateBinding SnapsToDevicePixels}\" HorizontalAlignment=\"{Binding Path=HorizontalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}\" VerticalAlignment=\"{Binding Path=VerticalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}\" RecognizesAccessKey=\"True\"/></Border></Grid><ControlTemplate.Triggers><Trigger Property=\"IsMouseOver\" Value=\"true\"><Setter TargetName=\"Bd\" Property=\"Background\" Value=\"" + tabMouseOverColor +"\"/></Trigger><Trigger Property=\"IsSelected\" Value=\"true\"><Setter Property=\"Panel.ZIndex\" Value=\"1\"/><Setter TargetName=\"Bd\" Property=\"Background\" Value=\"" + tabItemSelectedBackground + "\"/></Trigger><MultiTrigger><MultiTrigger.Conditions><Condition Property=\"IsSelected\" Value=\"false\"/><Condition Property=\"IsMouseOver\" Value=\"true\"/></MultiTrigger.Conditions><Setter TargetName=\"Bd\" Property=\"BorderBrush\" Value=\"" + tabMouseOverBorderBrush + "\"/></MultiTrigger><Trigger Property=\"TabStripPlacement\" Value=\"Bottom\"><Setter TargetName=\"Bd\" Property=\"BorderThickness\" Value=\"1,0,1,1\"/></Trigger><Trigger Property=\"TabStripPlacement\" Value=\"Left\"><Setter TargetName=\"Bd\" Property=\"BorderThickness\" Value=\"1,1,0,1\"/></Trigger><Trigger Property=\"TabStripPlacement\" Value=\"Right\"><Setter TargetName=\"Bd\" Property=\"BorderThickness\" Value=\"0,1,1,1\"/></Trigger><MultiTrigger><MultiTrigger.Conditions><Condition Property=\"IsSelected\" Value=\"true\"/>            <Condition Property=\"TabStripPlacement\" Value=\"Top\"/></MultiTrigger.Conditions><Setter Property=\"Margin\" Value=\"-2,-2,-2,-1\"/><Setter TargetName=\"Content\" Property=\"Margin\" Value=\"0,0,0,1\"/></MultiTrigger><MultiTrigger><MultiTrigger.Conditions><Condition Property=\"IsSelected\" Value=\"true\"/><Condition Property=\"TabStripPlacement\" Value=\"Bottom\"/></MultiTrigger.Conditions><Setter Property=\"Margin\" Value=\"-2,-1,-2,-2\"/><Setter TargetName=\"Content\" Property=\"Margin\" Value=\"0,1,0,0\"/></MultiTrigger><MultiTrigger><MultiTrigger.Conditions><Condition Property=\"IsSelected\" Value=\"true\"/><Condition Property=\"TabStripPlacement\" Value=\"Left\"/></MultiTrigger.Conditions><Setter Property=\"Margin\" Value=\"-2,-2,-1,-2\"/><Setter TargetName=\"Content\" Property=\"Margin\" Value=\"0,0,1,0\"/></MultiTrigger><MultiTrigger><MultiTrigger.Conditions><Condition Property=\"IsSelected\" Value=\"true\"/><Condition Property=\"TabStripPlacement\" Value=\"Right\"/></MultiTrigger.Conditions><Setter Property=\"Margin\" Value=\"-1,-2,-2,-2\"/><Setter TargetName=\"Content\" Property=\"Margin\" Value=\"1,0,0,0\"/></MultiTrigger><Trigger Property=\"IsEnabled\" Value=\"false\"><Setter TargetName=\"Bd\" Property=\"Background\" Value=\"" + tabItemDisabledBackground +"\"/><Setter TargetName=\"Bd\" Property=\"BorderBrush\"        Value=\""+tabItemDisabledBorderBrush+"\"/><Setter Property=\"Foreground\" Value=\"{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}\"/></Trigger></ControlTemplate.Triggers></ControlTemplate>";

            MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
            ParserContext pc = new ParserContext();
            pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            pc.XmlnsDictionary.Add("theme", "clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero");
            var btnTemplate = (ControlTemplate)XamlReader.Load(sr, pc);

            return btnTemplate;
        }

        public static TabItem SetBackgroundColor(this TabItem chain, Brush brush)
        {
            Brush newBrush = (brush);
            chain.Background = newBrush;
            return chain;
        }

        public static TabItem Initialize(this TabItem source, double width, double height, ContainerType containerType)
        {   
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.Width = width;
            source.Height = height;
            source.SetContainerType(containerType);
            IComposite icomp = CompositeTypeHelper.CreateIComposite<TabItem>(source);
            source.SetIComposite<TabItem>(icomp);

            //STORE SETTING BUILD CHAIN AND GET CHAIN?!?  Put these in a Composite implements an interface
            TabItemParent gbsr = new TabItemParent();
            source.StoreSetting<TabItem>("IParent", gbsr);

            source.StoreSetting<TabItem>("ObsCol", new ObservableCollection<Border>());

            source.AddItemBorder(Brushes.Gray, new Thickness(1, 1, 1, 1)); //default Border     
            source.StoreSetting<TabItem>("BorderSettings", new BorderSettings() { Width = double.NaN, Height = double.NaN, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(0, 0, 0, 0), CornerRadius = new CornerRadius(0), Padding = new Thickness(0) } );

            return source;
        }
        
    }//end of class
}//end of namespace
